// packageH/installationSever/installationSeverMemberHome/installationSeverMemberHome.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    codeShow: false,
    pictureShow: false,
    pictureList: [],

    titleArr: [
      {
        code: "all",
        name: "全部订单",
      },
      {
        code: "waitPay",
        name: "待支付",
      },
      {
        code: "waitWorker",
        name: "待分配",
      },
      {
        code: "waitTake",
        name: "待取货订单",
      },
      {
        code: "waitDeliveryPay",
        name: "待到付订单",
      },
      {
        code: "waitInstall",
        name: "待安装订单",
      },
      {
        code: "isFinish",
        name: "已完成订单",
      },
      {
        code: "refund",
        name: "售后订单",
      },
    ],
    custom_name: '',//安装自定义名称

    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    listData: [],
    networkLoading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.status) {
      this.setData({
        active: Number(options.status)
      });
    }
    this.getListData();
  },

  initData() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      networkLoading: false,
    });
  },

  getListData() {
    this.initData();
    let { titleArr, active, page } = this.data;
    let json = { code: titleArr[active].code, page };
    let urlStr = app.getNetAddresss("plugin.live-install.frontend.order.orderList");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.data.total_page = res.data.last_page;
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        if(res.data.another_name && res.data.another_name.plugin_name){
          wx.setNavigationBarTitle({
            title: res.data.another_name.plugin_name
          });
        }
        this.setData({
          custom_name: res.data.another_name.custom_name||'安装',
          listData: res.data.data,
          networkLoading: true,
        });
        this.data.titleArr.forEach((element,index) => {
          if(element.code == 'waitInstall') {
            this.setData({
              ['titleArr['+ index + "].name"]: `待${this.data.custom_name}订单`
            })
          }
        });
        console.log(res);
      },
    });
  },
  //加载更多数据
  _getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      // that.loading = true;
      return;
    } else {
      this.data.page += 1;
      let { titleArr, active, page } = this.data;
      let json = { code: titleArr[active].code, page };
      let urlStr = app.getNetAddresss("plugin.live-install.frontend.order.orderList");
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            let listData = this.data.listData.concat(res.data.data);
            this.setData({ listData });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        },
      });
    }
  },

  gotoDetails(evt) {
    let { order_id } = evt.currentTarget.dataset;
    wx.navigateTo({
      url: "/packageH/installationSever/installationSeverRefund/installationSeverRefund?order_id=" + order_id,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  async bindBtnClick(evt) {
    let { btn, order_id } = evt.currentTarget.dataset;

    //有些按钮需要弹窗二次确认
    if (btn.code == "orderClose") {
      let confirmFlag = await app.confirm(`是否确定${btn.name}`);
      if (confirmFlag == false) return;
    }

    if (btn.code == "orderPay") {
      wx.navigateTo({
        url: "/packageD/buy/orderPay/orderPay?order_id=" + order_id + "&status=2",
      });
    } else if (btn.code == "comment") {
      wx.navigateTo({
        url: "/packageD/member/myEvaluation/myEvaluation",
      });
    } else if (btn.code == "orderApplyRefund") {
      wx.navigateTo({
        url: "/packageD/member/myOrder/refund/refund?type=1&refundType=installationSever&order_id=" + order_id,
      });
    } else if (btn.code == "orderClose") {
      this.cancelOrder(order_id);
    }
    console.log(btn);
    //if(btn)
  },

  cancelOrder(order_id) {
    let urlStr = app.getNetAddresss("order.operation.close");
    app._postNetWork({
      url: urlStr,
      data: { order_id },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        wx.navigateBack();
      },
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  bindChangeActive(evt) {
    this.setData({
      active: evt.detail.index,
    });
    this.getListData();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
